package com.jiyinit.web.core.business.service;

import com.jiyinit.web.core.business.dao.TabCoreTaskDao;
import com.jiyinit.web.core.business.dao.TabUserNoticeDao;
import com.jiyinit.web.core.business.dto.BulewallRequest;
import com.jiyinit.web.core.business.entity.TabCoreTask;
import com.jiyinit.web.core.business.enums.CommonEnum;
import com.jiyinit.web.core.business.enums.SystemConstant;
import com.jiyinit.web.core.business.vo.NoticeUserVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by lr on 2018/1/3.
 */
@Service("bulewallServiceImpl")
public class BulewallServiceImpl {
    @Resource(name = "core.tabCoreTaskDao")
    private TabCoreTaskDao tabCoreTaskDao;

    @Resource(name="send.message")
    private ReadMessageServiceImpl readMessageService;

    @Resource(name = "core.userTaskServiceImpl")
    private UserTaskServiceImpl userTaskService;

    @Resource(name ="core.jpushClientServiceImpl")
    private JpushClientServiceImpl jpushClientService;

    @Resource(name = "core.tabUserNoticeDao")
    private TabUserNoticeDao tabUserNoticeDao;

    public int add(BulewallRequest request){
        //添加任务
        TabCoreTask bulewall = new TabCoreTask();
        bulewall.setTitle(request.getTitle());
        bulewall.setDescription(request.getContent());
        bulewall.setTaskCla(SystemConstant.BLUE_WALL);
        bulewall.setCreateTime(new Date());
        bulewall.setCreateUid(request.getUserId());
        bulewall.setIsDel(CommonEnum.IsDelete.COMMON.getCode());
        int num = tabCoreTaskDao.addTask(bulewall);
        //添加人员
        userTaskService.addUsers(request.getUserId(),request.getAlloterIds(),bulewall.getTaskId());
        //未读表
        readMessageService.add(request.getUserId(),request.getAlloterIds(),SystemConstant.BLUE_WALL,bulewall.getTaskId());
        //极光推送
        HashMap<String,String> sendMessages = new HashMap<>();
        sendMessages.put("content","新建蓝墙");
        sendMessages.put("id",bulewall.getTaskId().toString());

        String[] users = new String[request.getAlloterIds().length];
        for (int i = 0; i <request.getAlloterIds().length ; i++) {
            users[i] = request.getAlloterIds().toString();
        }

        jpushClientService.send(request.getChannel(),sendMessages,users);

        return num;
    }

    public int delete(BulewallRequest request){
        int num = 0;
        if(request.getId() != null) {
            Integer[] id = new Integer[1];
            id[0] = request.getId();
            num = tabCoreTaskDao.deleteTaskByIds(id);
        }
        return num;
    }

    public TabCoreTask detail(Integer id){
        return tabCoreTaskDao.findTaskById(id);
    }

    public List<NoticeUserVo> findAlloters(Integer noticeId){
        return tabUserNoticeDao.findAlloterById(noticeId);
    }
}
